package com.love.marriage.personalImageryInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.oss.service.FileInfoService;
import com.love.marriage.oss.vo.FileInfoVo;
import com.love.marriage.personalImageryInfo.entity.PersonalImageryInfo;
import com.love.marriage.personalImageryInfo.mapper.PersonalImageryInfoMapper;
import com.love.marriage.personalImageryInfo.service.PersonalImageryInfoService;
import com.love.marriage.personalImageryInfo.vo.PersonalImageryInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  个人形象表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-13 22:54:18
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class PersonalImageryInfoServiceImp extends ServiceImpl<PersonalImageryInfoMapper, PersonalImageryInfo> implements PersonalImageryInfoService {

    private final PersonalImageryInfoMapper personalImageryInfoMapper;

    private final FileInfoService fileInfoService;

    @Override
    public Page<PersonalImageryInfoVo> getPage(Long pageNum, Long pageSize, PersonalImageryInfoVo personalImageryInfoVo) {
        Page<PersonalImageryInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return personalImageryInfoMapper.getPage(page, personalImageryInfoVo);
    }

    @Override
    public List<PersonalImageryInfoVo> getList(PersonalImageryInfoVo personalImageryInfoVo) throws Exception {
        List<PersonalImageryInfoVo> list = personalImageryInfoMapper.getList(personalImageryInfoVo);
        if (!list.isEmpty()) {
            list.forEach(item -> {
                FileInfoVo fileInfoVo;
                try {
                    fileInfoVo = fileInfoService.queryFileInfo(item.getFileId());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                item.setFileInfoVo(fileInfoVo);
            });
        }
        return list;
    }

    @Override
    public PersonalImageryInfoVo queryPersonalImageryInfo(Long id) {
        return personalImageryInfoMapper.queryPersonalImageryInfo(id);
    }

    @Override
    public PersonalImageryInfoVo addPersonalImageryInfo(PersonalImageryInfoVo personalImageryInfoVo, MultipartFile file) throws Exception {
        FileInfoVo fileInfoVo = fileInfoService.addFileInfo("common", file);
        PersonalImageryInfo personalImageryInfo = new PersonalImageryInfo();
        BeanUtils.copyProperties(personalImageryInfoVo, personalImageryInfo);
        personalImageryInfo.setFileId(fileInfoVo.getId());
        personalImageryInfoMapper.insert(personalImageryInfo);
        PersonalImageryInfoVo res = new PersonalImageryInfoVo();
        BeanUtils.copyProperties(personalImageryInfo, res);
        res.setFileInfoVo(fileInfoVo);
        return res;
    }

    @Override
    public PersonalImageryInfoVo updatePersonalImageryInfo(PersonalImageryInfoVo personalImageryInfoVo) {
        PersonalImageryInfo personalImageryInfo = new PersonalImageryInfo();
        BeanUtils.copyProperties(personalImageryInfoVo, personalImageryInfo);
        personalImageryInfoMapper.updateById(personalImageryInfo);
        return personalImageryInfoVo;
    }

    @Override
    public Boolean deletePersonalImageryInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        personalImageryInfoMapper.deleteBatchIds(idArr);
        return true;
    }
}
